#!/usr/bin/env python
# coding: utf-8
import  sys
import  os
import codecs
import time
from task.taskItem import *
from task.tasklist import *
from  engine.common import *
import datetime
#检测这个目录下有没有 send 开头的目录，目录里面有没有图片和文字 desc.txt
path = sys.argv[1]
#服务器文件path   test.dianxi.com。  eg /data/wwwroot/test.dianxi.com/xxxxx
serverPath = sys.argv[2]

#run create template code  postion

runScriptPath = sys.argv[3]


fixDir = [os.getcwd()+'/cut/nokiax5',os.getcwd()+'/shouji/nokiax5/taskCommon',os.getcwd()+'/task']
fixDir = ['/home/u18/work/handset/handset/shouji/nokiax5/192.168.0.130/friendInfo/月野兔',"/home/u18/work/handset/handset/shouji/nokiax5/192.168.0.130/friendInfo/点点lucky","/home/u18/work/handset/handset/shouji/nokiax5/192.168.0.207/friendInfo/溜溜小可爱"]

currentDir=""

def compare(x, y):
    stat_x = os.stat(currentDir + "/" + x)

    stat_y = os.stat(currentDir + "/" + y)

    if stat_x.st_ctime < stat_y.st_ctime:
        return -1
    elif stat_x.st_ctime > stat_y.st_ctime:
        return 1
    else:
        return 0

items=[]
for xpath in fixDir:
    tempItems = os.listdir(xpath)
    for ypath in tempItems:
        if os.path.isdir(xpath+"/"+ypath):
            items.append(xpath+"/"+ypath)

dir_list = sorted(items,key=lambda x: os.path.getmtime(x),reverse=True)
# print(items)
# for x in items:
#     print( x)

print("*"*100)


found = ""
for x in dir_list:
    print( x)

    desc = False
    tkl = False
    image = False
    tempItems = os.listdir(x)
    for xx in tempItems:
        print( xx)
        if 'desc.txt' in xx:
            desc = True
        if "tkl.txt" in xx:
            tkl = True
        if 'jpg' in xx or 'png' in xx or 'jpeg' in xx:
            image = True
    if desc == True and tkl==True and image == True:
        print("all true")
        found = x
        print(x)
        break



if found == "":
    print('not found')
    exit(1)
else:
    print(found)



os.system("mkdir "+path+"/sendnow")
os.system("cp "+found+"/*.* "+path+"/sendnow/")
os.system("mv "+found+"/tkl.txt "+found+"/tksl.txt")


#path = os.getcwd()+"/test/"            jianguoyun dir
#serverPath = os.getcwd()+"/test2/"   /data/wwwroot/test.dianxi.com/



#runScriptPath = os.getcwd()+"/"   /home/u18/work/handset/handset/shouji/nokiax5/task/192.168.0.137/runned


print(path)
print(serverPath)
print(runScriptPath)
def getToday():
    now = datetime.datetime.now()
    return now.strftime('%Y-%m-%d-%H-%M')



def getTaskFloder(path,serverPath):
    isValImg = False
    isValDesc = False
    sendName = ""
    countImg = 0
    file_list = os.listdir(path)

    for file in file_list:
        print(path+file)
        isValImg = False
        isValDesc = False
        countImg = 0
        if 'send' in file and os.path.isdir(path+file):
            sendName = file
            img_list = os.listdir(path+file)
            for x in img_list:
                if 'jpg' in x  or 'png' in x or 'jpeg' in x:
                    isValImg = True
                    countImg = countImg+1
                if "desc.txt" in x:
                    isValDesc = True
    if isValDesc and isValImg:
        print("有效"+sendName)
        print("有" + str(countImg))

        #mkdir path with datetime and cp it in it
        if not os.path.exists(path+"../已经运行/"+sendName+getToday()):
            os.makedirs(path+"../已经运行/"+sendName+getToday())
        cmd = "cp -r "+path+"/"+sendName+ " "+path+"../已经运行/"+sendName+getToday()
        os.system(cmd)

        cmd = "rm -rf  "  + serverPath +sendName
        os.system(cmd)

        cmd = "mv "+path+"/"+sendName+"  "+serverPath
        os.system(cmd)
        cmd = "sudo chmod 777  "+serverPath+" -R"
        os.system(cmd)
    return sendName,countImg


floderName,imgCount=getTaskFloder(path ,serverPath)
print("*"*90)
print(floderName)
print(imgCount)
if imgCount >1:
    para = "执行 "*imgCount
    os.chdir(runScriptPath)
    print(runScriptPath)
    cmd =  "python3 readConfig.py 发朋友圈template.ini "  +floderName+ " "+para
    print(cmd)
    os.system(cmd)
